package com.aps.server.controller;

import com.aps.common.entity.Result;
import com.aps.server.dto.ApsCraftDto;
import com.aps.server.entity.ApsCraft;
import com.aps.server.entity.ApsOperation;
import com.aps.server.service.ApsCraftService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工艺表 前端控制器
 * 
 * @author aps
 * @since 2024-01-01
 */
@Slf4j
@Tag(name = "工艺管理", description = "工艺相关接口")
@RestController
@RequestMapping("/api/craft")
@CrossOrigin
public class ApsCraftController {

    @Resource
    ApsCraftService apsCraftService;

    /**
     * 按产品、工艺信息 查询对应工艺列表数据
     * @param productCode
     * @param productName
     * @param craftName
     * @param craftCode
     * @return
     */
    @Operation(summary = "获取工艺列表", description = "分页查询工艺列表")
    @GetMapping("/query-craft-by-tenantid")
    public Result<List<ApsCraftDto>> queryOperationByCraftCode(
            @RequestParam(value = "productCode" , required = false) String productCode ,
            @RequestParam(value = "productName" , required = false) String productName ,
            @RequestParam(value = "craftName" , required = false) String craftName ,
            @RequestParam(value = "craftCode" , required = false) String craftCode
    ) {
        Result<List<ApsCraftDto>> result = new Result<>();
        try {
            List<ApsCraftDto> s = apsCraftService.getCraftsByTenantId(productCode,productName,craftName,craftCode);
            result.setData(s);
            result.setSuccess(true);
        }
        catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("查询工艺数据失败");
        }
        return result;
    }

    @Operation(summary = "新增工艺", description = "新增工艺信息")
    @PostMapping("/create")
    public Result<String> createCraft(@RequestBody ApsCraft apsCraft) {
        // TODO: 实现新增逻辑
        if(apsCraft.getCraftCode() == null){
            return Result.error(400, "工艺信息创建失败,缺少工艺编码");
        }
        try {
            boolean success = apsCraftService.createCraft(apsCraft);
            if (success) {
                return Result.success(apsCraft.getId().toString(), "工艺信息创建成功");
            } else {
                return Result.error(400, "工艺信息创建失败");
            }
        } catch (Exception e) {
            log.error("创建工艺信息失败", e);
            return Result.error(500, "创建工艺信息失败: " + e.getMessage());
        }
    }

    @Operation(summary = "更新工艺", description = "更新工艺信息")
    @PostMapping("/modify")
    public Object update(@RequestBody ApsCraft apsCraft) {
        // TODO: 实现更新逻辑
        try {
            String id = apsCraft.getId();
            if(id == null) {
                return Result.error(400, "数据错误","未获取到数据id！");
            }
            apsCraft.setId(id);
            boolean success = apsCraftService.updateCraft(apsCraft);
            if (success) {
                return Result.success("工艺信息更新成功");
            } else {
                return Result.error(400, "工艺信息更新失败");
            }
        } catch (Exception e) {
            log.error("更新工艺信息失败", e);
            return Result.error(500, "更新工艺信息失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除工艺", description = "根据ID删除工艺")
    @GetMapping("/del/{id}")
    public Object delete(@PathVariable String id) {

        try {
            boolean success = apsCraftService.deleteCraft(id);
            if (success) {
                return Result.success("工艺信息删除成功");
            } else {
                return Result.error(400, "工艺信息删除失败");
            }
        } catch (Exception e) {
            log.error("删除工艺信息失败", e);
            return Result.error(500, "删除工艺信息失败: " + e.getMessage());
        }
    }

}
